//
//  YYPictureViewController.m
//  Style
//
//  Created by dllo on 15/10/6.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "YYPictureViewController.h"
#import "YYAnimationViewController.h"

@interface YYPictureViewController ()

@end

static CGRect oldframe;

@implementation YYPictureViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showImage:self.strOriginalImgUrlView];
}

- (void)showImage:(UIImageView *)avatarImageView{
    
    UIImage *image=avatarImageView.image;
    //    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:self.view];
    backgroundView.backgroundColor=[UIColor blackColor];
    [YYAnimationViewController fadeIn:backgroundView andAnimationDuration:0.1 andWait:YES];
    backgroundView.alpha= 0.9;
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];
    
    imageView.image=image;
    imageView.tag=1;
    [backgroundView addSubview:imageView];
    [self.view addSubview:backgroundView];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer:tap];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(saveImage:)];
    [backgroundView addGestureRecognizer:longPress];
    
    imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
    
}

- (void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.1 animations:^{
        imageView.frame=oldframe;
        backgroundView.alpha=0;
        [self.navigationController popViewControllerAnimated:NO];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)saveImage:(UILongPressGestureRecognizer *)saveImage
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"保存到本地相册" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UIImageWriteToSavedPhotosAlbum(self.strOriginalImgUrlView.image, nil, nil, nil);
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 20 * 7.5, self.view.frame.size.height / 2, self.view.frame.size.width / 20 * 5, 40)];
        label.text = @"保存成功";
        label.layer.cornerRadius = 8;
        label.layer.masksToBounds = YES;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor grayColor];
        [label.layer addAnimation:[self opacityForever_Animation:0.5] forKey:nil];
        [self.view addSubview:label];
        label.alpha = 0;
        [alert dismissViewControllerAnimated:YES completion:^{
        }];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

/* 动画设置 */
-(CABasicAnimation *)opacityForever_Animation:(float)time
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.1f];//这是透明度。
    animation.autoreverses = YES;
    animation.duration = time;
    animation.repeatCount = 0;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
    return animation;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
